// pages/user/user-coupon.js

import {obj_coupon } from '../../util/model.js';
import { RequestManager } from '../../util/requestManager.js'
let requestManager = new RequestManager()
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    conentMarginTop: 0,
    isPhoneX: false,
    isSelected:false,
    isNoCoupon:true,
    couponItems: [],
    cardNumber:'',
    cardPassword:''
  },
  onReady:function(){
    this.alertview = this.selectComponent("#alertview");
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var title = "我的优惠券";
    if (typeof (options.selected) != "undefined") {
      title = "选择优惠券";
      this.setData({
        isSelected: true
      })
    }
    
    this.setData({
      isPhoneX: app.globalData.isIphoneX,
      conentMarginTop: app.globalData.navHeight
    })

    wx.setNavigationBarTitle({
      title: title,
    })

    this.private_downloadData()
  },

  //  获取数据
  private_downloadData: function () {
    var that = this;
    requestManager.getUserCouponList(app.globalData.openid,(reslut) => {
      that.setData({
        couponItems: reslut
      })
    });
  },

  //  action methods
  //  不选择优惠券
  action_no_coupon:function(event){
    let is_selected = event.detail.checked;
    var tmpItems = this.data.couponItems;
    tmpItems.forEach((tmpItem) => {
      tmpItem.selected = false;
    })
    this.setData({
      couponItems: tmpItems,
      isNoCoupon:is_selected
    })
  },
  //  选择对应的优惠券
  action_selected:function(event){
    let is_selected = event.detail.checked;
    let key = event.detail.key;
    var tmpItems = this.data.couponItems;
    tmpItems.forEach((tmpItem) => {
      if (tmpItem.id == key) {
        tmpItem.selected = is_selected;
      }
      else {
        tmpItem.selected = false;
      }
    })
    this.setData({
      couponItems: tmpItems,
      isNoCoupon: false
    })
  },
  action_save:function(event){

    var selectedItem = null;

    if(!this.data.isNoCoupon){
      var tmpItems = this.data.couponItems;
      tmpItems.forEach((tmpItem) => {
        if (tmpItem.selected === true) {
          selectedItem = tmpItem
        }
      })
    }

    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];

    if (selectedItem != null) {
      // let coupon = new obj_coupon(selectedItem)
      prevPage.setData({
        coupontItem: selectedItem,
        is_selected_coupon: true
      })
    }
    else {
      prevPage.setData({
        coupontItem: null,
      })
    }

    wx.navigateBack({
      delta: 1
    })
  },
  action_add:function(event){
    this.alertview.showAlertView();
  },
  action_cancelEvent:function(event){
    this.setData({
      cardNumber:'',
      cardPassword:''
    })
  },
  action_confirmEvent:function(event){

    var that = this;
    setTimeout(function(){
      that.private_submitData();
    },300);
  },
  //  文本结束编辑
  textDidEndEdit:function(event){
    let id = event.currentTarget.id
    let value = event.detail.value
    if(id === "cardPassword"){
      this.setData({
        cardPassword:value
      })
    }
    else{
      this.setData({
        cardNumber: value
      })
    }
  },
  //
  private_submitData(){
    // console.log("卡号:", this.data.cardNumber, "密码:", this.data.cardPassword)
    if (this.data.cardNumber.length === 0) {

      wx.showToast({
        title: '卡号不能为空',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (this.data.cardPassword.length === 0) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    var that = this;
    requestManager.bindUserCouponItem(app.globalData.openid, this.data.cardNumber, this.data.cardPassword, result => {
      wx.showToast({
        title: '兑换成功',
        icon: 'success',
        duration: 1500
      })
      that.private_downloadData();
      that.setData({
        cardPassword: '',
        cardNumber: ''
      })
    });
  }
})